<template>
  <div class="login_container">
    <div class="login_box">
      <!-- img -->
      <div class="login_imgbox">
        
      </div>
      <!-- 登录 -->
      <el-form ref="<login_formref></login_formref>" label-width="0px" class="login_form" v-model="Loginform" :rules="LoginformRules">
        <!-- 名称 -->
        <el-form-item label="" prop="username">
          <el-input prefix-icon="el-icon-s-custom" v-model="Loginform.username" ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="" prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="Loginform.password" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="loginGo">登录</el-button>
          <el-button type="infor" @click="redstLoginfrom">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {login} from '@/api/getData'

export default {
  data () {
    return {
      Loginform: {
        username: 'lulinjuan',
        password: 'An123456'
      },
      // 验证规则
      LoginformRules: {
        username: [
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' },
          { required: true, message: '请输入登录名称', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置
    redstLoginfrom () {
      this.$refs.login_formref.resetFields()
    },
    // 登录 自动登录方法 1.查询本地用户信息表 是否存在,如有数据 将用户名和密码取出 赋值到页面  调用loginGo方法
    async loginGo () {
      const res = await login({ strUserID: this.Loginform.username, passWord: this.Loginform.password })
      console.log(res)
      if (res.errcode === 200) {
        this.$message({
          type: 'success',
          message: res.errmsg
        })
        res.data.list[0].passWordCode = this.Loginform.password
        this.$store.islogin = true
        this.$store.token = res.data.auth
        console.log(this.$token)
        this.$router.push('/home')
        // 登录成功  保存本地数据库 用户信息 并追加页面的密码字段
        // 保存到本地数据库 res.data.list[0]
        // 其次把res.data.list[0]赋值到全局变量用于 查询等使用
        // token 赋值到全局对象 res.data.auth
      } else if (res.errcode === 406) {
        this.$message({
          type: 'error',
          message: res.errmsg
        })
      } else {
        this.$message({
          type: 'error',
          message: '接口异常!'
        })
      }
    }
  }
}
</script>

<style scoped>
  .login_form {
    position: absolute;
    bottom: 0;

    width: 100%;
    padding: 0 16px;
    box-sizing: border-box;
  }

  .btns {
    display: flex;
    justify-content: flex-start;
    padding-left: 135px;
  }

  .login_container {
    background-color: #2b4b6b;
    height: 100%;
  }

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }

  .login_imgbox {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
  }

  .login_imgbox img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #fff;
  }
</style>
